<?php
date_default_timezone_set('Asia/Shanghai');
set_time_limit(0);
ignore_user_abort(TRUE);

require_once './vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
define('IN_ECS', true);

require(dirname(__FILE__) . '/cls_mysql.php');

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "AC7eeee8029a31d4c86ebb0b6c2728c86e";
$token = "d6badaeebdeb7b5d6474887bd25e3ef2";
$client = new Client($sid, $token);

$db = new cls_mysql("34.208.3.243", "kuaisong_api1", "FZP5bw6XRCDtXzHs","kuaisong","utf8");

//$sql = 'select * from tblsendmms where nickname = "Fearlessの黑郁金香"';

$sql = 'select  DISTINCT mobile from tblsendmms order by mobile asc';

$all = $db->getAll($sql);

$suc = 0;
$fail = 0;
echo "start:".date("Y-m-d H:i:s",time())."<br/>";

$sql = 'select  mobile from ecs_sms_send_log ';
$mobiless = $db->getAll($sql);

$mobiles_arr = array();
foreach($mobiless as $k=>$v){
    $mobiles_arr[]= $v['mobile'];
}

//发送之前测试
//$all = array(
//    array('mobile'=>'7786970669'),
//    array('mobile'=>'6043642383')
//);

foreach($all as $k=>$v){
    if(array_search($v['mobile'], $mobiles_arr)!==FALSE){
        continue;
    }
    $msg = '祝贺快送平台上线500天破15万单里程碑，大温全区18日再启免费送！欢乐送不完，假日乐翻天！';
    try {
        $rs = $client->messages
        ->create(
            '+1'.$v['mobile'],
            array(
                "from" => "+17786558663",
                "body" => $msg,
                "mediaUrl" => "https://staging.quitesoondelivery.com/ads/images/festival.jpg"
            )
        );
        $suc++;
        sleep(1);
        $arr = array(
            'mobile'=>$v['mobile'],
            'status'=>1,
            'add_time'=>date("Y-m-d H:i:s",time())
        );
        $db->autoExecute('ecs_sms_send_log', $arr);
    } catch (Exception $e) {
        print "The ".$k." number fail:";
        $fail++;
        print $e->getMessage()."<br/>";
        $arr = array(
            'mobile'=>$v['mobile'],
            'status'=>0,
            'add_time'=>date("Y-m-d H:i:s",time())
        );
        $db->autoExecute('ecs_sms_send_log', $arr);
    }
    
}
echo "fail:$fail,success:$suc";
echo "end:".date("Y-m-d H:i:s",time())."<br/>";